package com.baseinfo.service.impl;
import com.model.TContract;
import com.baseinfo.service.ContractService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.framework.util.StringUtil;
import com.framework.base.dao.impl.BaseDaoImpl;
/**
* @author admin
* @date 
*/
@Service
public class ContractServiceImpl implements ContractService{
    @Resource(name = "baseDaoImpl")
    private BaseDaoImpl baseDao;

    @Override
    public TContract save (TContract modelParam) {
    TContract model = null;
    if (modelParam != null) {
    try {
    model = (TContract) this.baseDao.savePo(modelParam);
    } catch (Exception e) {
    System.out.println("更新失败");
    }
    }
    return model;
    }

    @Override
    public TContract saveOrupdate (TContract modelParam) {
    TContract model = null;
    if (modelParam != null) {
    try {
    model = (TContract) this.baseDao.saveOrupdate(modelParam);
    } catch (Exception e) {
    System.out.println("更新失败");
    }
    }
    return model;
    }

    @Override
    public List<TContract> findAll (Map<String,Object> map) {
    String params = "";
    StringBuffer hql = new StringBuffer("from TContract c where 1=1 and c.isDeleted=0");
    if (map != null) {
    params = this.SQLAddParams(map);
    hql = hql.append(params);
    }
    List<TContract> list = null;
    try {
    list = this.baseDao.listByHql(hql.toString(), null);
    } catch (Exception e) {
    e.printStackTrace();
    System.out.println("条件查找失败");
    }
    return list;
    }

    @Override
    public TContract findById (Long id) {
    TContract model = null;
    List<Object> params = new ArrayList<Object>();
    if (id != null) {
    StringBuilder hsql = new StringBuilder("from TContract c where 1=1 and c.isDeleted=0 ");
    params.add(id);
    hsql.append(" and c.id=?");
    model = (TContract) this.baseDao.getSingleByHsql(hsql.toString(), params);
    } else {
    System.out.println("查找不到ID=" + id + "的数据");
    }
    return model;
    }

    @Override
    public void del (Long id) {
    List<Object> params = new ArrayList<Object>();
        if (id != null) {
        params.add(id);
        try {
    TContract model = (TContract) this.baseDao.getSingleByHsql(" from TContract c where c.id=? and c.isDeleted=0 ", params);
        model.setIsDeleted(1);
        this.baseDao.saveOrupdate(model);
        } catch (Exception e) {
        e.printStackTrace();
        }
        }
        }


        /**
        * 搜索SQL加参数条件（抽出来复用）
        *
        * @param map
        * @return
        */
        public String SQLAddParams (Map<String,Object> map) {
        StringBuffer bf = new StringBuffer("");
        if (map.containsKey("name") && StringUtil.isNotEmpty(map.get("name").toString())) {
        bf.append(" and c.name like '%").append(map.get("name").toString()).append("%'");
        }

        return bf.toString();
        }

        /**
        * 根据元素全局查询(分页)
        */
        @Override
        public List<TContract> conditionsPage(Map<String,Object> map,Long start, Long length) {
            String params=this.SQLAddParams(map);
            StringBuffer hql=new StringBuffer("from TContract c where 1=1 and c.isDeleted=0 ");
            hql=hql.append(params);
            List<TContract> list=null;
                try{
                list=this.baseDao.listByHqlBeatch(hql.toString(), start, length);
                }catch(Exception e){
                e.printStackTrace();
                System.out.println("条件查找用户失败");
                }
                return list;
                }

        /**
        * 获取总记录数
        */
        @Override
        public Long getTotalSize() {
        return this.baseDao.getTotalSize("select count(c.id) from TContract c where 1=1 and c.isDeleted=0",null);
        }

        /**
        * 分页查询
        * @param start 第几页
        * @param length 一页几行
        * @return
        */
    @Override
    public List<TContract> searchPage(Long start, Long length) {
      return this.baseDao.listByHqlBeatch("from TContract c where 1=1 and c.isDeleted=0 ", start, length);
    }

        /**
        * 获取条件搜索到的条目数
        */
    @Override
    public Long getSearchSize(Map<String,Object> map) {
        String params=this.SQLAddParams(map);
        StringBuffer hql=new StringBuffer("select count(c.id) from TContract c where 1=1 and c.isDeleted=0");
        hql.append(params);
        return this.baseDao.getTotalSize(hql.toString(),null);
    }
}